package com.bootssm.controller;

import com.bootssm.dao.NotesMapper;
import com.bootssm.entity.Notes;
import com.bootssm.entity.NotesVO;
import com.bootssm.entity.PageView;
import com.bootssm.entity.Result;
import com.bootssm.service.NotesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notes")
public class NotesController {
    @Autowired
    NotesService notesService;

    @PostMapping("/findAll")
    public PageView<Notes> findAll(@RequestBody NotesVO notesVO) {
        Integer page = notesVO.getPage();
        Integer start = (page - 1) * notesVO.getSize();
        notesVO.setStart(start);
        List<Notes> notes = notesService.findAll(notesVO);
        Integer count = notesService.getCount(notesVO);

        PageView<Notes> pageView = new PageView<>();
        pageView.setDataList(notes);
        pageView.setCount(count);
        return pageView;
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody Notes notes) {
        try {
            notesService.insert(notes);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "save wrong!");
        }
        return new Result("200", "save!");
    }

    @GetMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") String noteid){
        try {
            notesService.deleteById(noteid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "delete wrong!");
        }
        return new Result("200", "delete!");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Notes notes){
        try {
            notesService.update(notes);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("500", "update wrong!");
        }
        return new Result("200", "update!");
    }

    @GetMapping("/findById/{id}")
    public Notes findById(@PathVariable("id") String noteid){
        return notesService.findById(noteid);
    }
}
